package com.fz.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by bai on 2017/6/29.
 */
public class TestInterceptor implements HandlerInterceptor {
    boolean fleg = false;

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception {
        Object mem = req.getSession().getAttribute("member");
        if (mem != null) {
            fleg = true;
        } else {
            resp.sendRedirect("/login.jsp");
        }

        return fleg;
    }

    @Override
    public void postHandle(HttpServletRequest req, HttpServletResponse resp, Object o, ModelAndView mv) throws Exception {
        System.out.println("postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest req, HttpServletResponse resp, Object o, Exception e) throws Exception {
        System.out.println("afterCompletion");
        req.getSession().removeAttribute("member");
    }
}
